<?php
/**
 * Created by PhpStorm.
 * User: JYuans
 * Date: 2019/12/10
 * Time: 16:09
 */
namespace Bstage\Controller;

class TeamExamineEmployingItemController extends BstageController {

    /**
     * 设置班子考评选人用人指标接口
     */
    public function setEmployingItem(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");

        $team_examine_data = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field("status")
            ->find();
        if(!$team_examine_data){
            $this->errorReturn("班子考评ID错误");
        }

        if($team_examine_data['status'] != 0){
            $this->errorReturn("该考评无法重新设置");
        }

        $item_json = $_REQUEST['item_json']?$_REQUEST['item_json']:$this->errorReturn("缺少指标json");

        $item_array = json_decode($item_json,true);

        $model = M();
        $model->startTrans();

        M("TeamExamineEmployingItem")
            ->where(['team_examine_id'=>$team_examine_id])
            ->delete();

        $item_num = 1;
        foreach($item_array as $key=>$value) {
            $new_data = array();
            $new_data['team_examine_id'] = $team_examine_id;
            $new_data['item_num'] = $item_num;
            $new_data['title'] = $value;

            $add_result = M("TeamExamineEmployingItem")
                ->add($new_data);
            if(!$add_result){
                $model->rollback();
                $this->errorReturn("添加失败，请重新尝试");
            }
            $item_num++;
        }

        $update_array = array();
        $update_array['employing_item_json'] = $item_json;
        $update_array['update_time'] = time();
        $update_examine = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->save($update_array);
        if(!$update_examine){
            $model->rollback();
            $this->errorReturn("保存指标json失败，请重新尝试");
        }

        $model->commit();
        $this->successReturn("保存成功");
    }

    /**
     * 获取班子考评选人用人指标列表接口
     */
    private function getEmployingItemList(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");

        $item_list = M("TeamExamineEmployingItem")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field("e_item_id,title")
            ->select();
        if(!$item_list){
            $this->returnJson(503,"无数据");
        }

        $this->successReturn("获取成功",$item_list);

    }

    /**
     * 添加班子考评选人用人指标接口
     */
    private function addEmployingItemAction(){
        $new_data = array();
        $new_data['team_examine_id'] = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");
        $new_data['title'] = $_REQUEST['title']?$_REQUEST['title']:$this->errorReturn("缺少指标类型名称");

        $add_result = M("TeamExamineEmployingItem")
            ->add($new_data);
        if(!$add_result){
            $this->errorReturn("添加考核指标失败");
        }
        $this->successReturn("添加成功",['e_item_id'=>$add_result]);
    }

    /**
     * 修改班子考评选人用人指标接口
     */
    private function editEmployingItemAction(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");
        $e_item_id = $_REQUEST['e_item_id']?$_REQUEST['e_item_id']:$this->errorReturn("缺少班子考评选人用人指标ID");

        $new_data = array();
        $new_data['title'] = $_REQUEST['title']?$_REQUEST['title']:$this->errorReturn("缺少指标类型名称");

        $update_result = M("TeamExamineEmployingItem")
            ->where(['e_item_id'=>$e_item_id,'team_examine_id'=>$team_examine_id])
            ->save($new_data);
        if(!$update_result){
            $this->errorReturn("修改失败");
        }
        $this->successReturn("修改成功");
    }

    /**
     * 删除班子考评选人用人指标接口
     */
    private function deleteEmployingItemAction(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");
        $id_str = $_REQUEST['id_str']?$_REQUEST['id_str']:$this->errorReturn("缺少班子考评选人用人指标ID");

        $delete_result = M("TeamExamineEmployingItem")
            ->where(['team_examine_id'=>$team_examine_id,'e_item_id'=>['in',$id_str]])
            ->delete();
        if(!$delete_result){
            $this->errorReturn("删除失败，请重新尝试");
        }
        $this->successReturn("删除成功");
    }

}